<template>
    <div id="app">
        <a-modal v-model:visible="visable" title="编辑任务" :mask-closable="false" :width="500" @before-ok="doSubmit()"
            @cancel="close">
            <a-form ref="appForm" :model="formData" :rules="formRule" :label-col-props="{ span: 6 }" layout="vertical">
                <a-row :gutter="8">
                    <a-col :span="14">
                        <a-form-item label="BEAN" label-position="top" field="beanName">
                            <a-input v-model="formData.beanName" :disabled="formData.taskId != ''"
                                placeholder="请输入Bean名称"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="10">
                        <a-form-item label="方法名称" label-position="top" field="methodName">
                            <a-input v-model="formData.methodName" :disabled="formData.taskId != ''"
                                placeholder="方法名称"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="8">
                    <a-col :span="15">
                        <a-form-item label="cron表达式" label-position="top" field="cronExpression">
                            <a-input v-model="formData.cronExpression" placeholder="0/10 * * * * ?"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="9">
                        <a-form-item label="状态" label-position="top" field="status">
                            <a-radio-group button-style="solid" v-model="formData.status" :false-value="0" :true-value="1">
                                <a-radio :value="1">正常</a-radio>
                                <a-radio :value="0">暂停</a-radio>
                            </a-radio-group>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="8">
                    <a-col :span="12">
                        <a-form-item label="备注说明" label-position="top" field="remark">
                            <a-textarea v-model="formData.remark" placeholder="请输入备注说明" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="方法参数" label-position="top" field="methodParams">
                            <a-textarea v-model="formData.methodParams" placeholder="方法执行传参(字符串)" />
                        </a-form-item>
                    </a-col>
                </a-row>

            </a-form>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { onBeforeMount, ref } from 'vue';
import utils from '@/utils/utils';
import request from '@/config/request';

export default {
    setup(props, { emit }) {
        const visable = ref(false);
        const appForm = ref();
        const formData = ref({
            taskId: ref(''),
            beanName: ref(''),
            methodName: ref(''),
            methodParams: ref(''),
            cronExpression: ref(''),
            remark: ref(''),
            status: ref(1),
        });
        const formRule = ref({
            beanName: [
                { required: true, message: '请输入Bean名称' }
            ],
            methodName: [
                { required: true, message: '请输入方法名称' }
            ],
            cronExpression: [
                { required: true, message: '请输入Cron表达式' }
            ],
            remark: [
                { required: true, message: '请您写个备注好吗?' }
            ]
        });
        const show = (form: any) => {
            visable.value = true;
            appForm.value.resetFields();
            formData.value.taskId = '';
            if (form) {
                formData.value = JSON.parse(JSON.stringify(form));;
            }
        };
        const close = () => {
            visable.value = false;
        };
        const doSubmit = () => {
            return new Promise((resolve, reject) => {
                appForm.value.validate((valid: any) => {
                    if (valid) {
                        resolve(false); return;
                    }
                    utils.loading();
                    request.post('/platform/task/save', formData.value).then((ret: any) => {
                        if (ret.code == 200) {
                            utils.success('操作成功！').then(() => {
                                appForm.value.resetFields();
                                visable.value = false;
                                emit('success');
                            })
                        }
                        resolve(true)
                    }).catch(err => {
                        resolve(false)
                    });
                })
            })
        };
        return { visable, appForm, formData, formRule, show, close, doSubmit }
    },
}
</script>
